package com.honglang.module.album.action;

import java.util.ArrayList;
import java.util.List;

import com.honglang.common.tools.Constants;
import com.honglang.common.tools.Pager;
import com.honglang.module.album.entity.Album;
import com.honglang.module.album.entity.AlbumPhoto;
import com.honglang.module.album.service.AlbumPhotoService;
import com.honglang.module.album.service.AlbumService;
import com.honglang.module.album.service.PhotoService;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;

public class AlbumAction extends BaseAction{
	
	private AlbumService albumService;
	private AlbumPhotoService albumPhotoService;
	private PhotoService photoService;
	
	
	/**
	 * get one page of album
	 * @return
	 */
	public String getAlbumPage(){
		String pageNoStr = this.getRequest().getParameter("pageNo");
		int pageNo = (null != pageNoStr) ?Integer.valueOf(this.getRequest().getParameter("pageNo")): Constants.DEFAULT_PAGE_NO;
		Pager pager =  this.albumService.getPager(Constants.PHOTO_PAGE_SIZE, pageNo);
		List<AlbumPhoto> albumPhotoList = new ArrayList<AlbumPhoto>();
		for(Object album : pager.getResultList()){
			List<AlbumPhoto> list = this.getAlbumPhotoService().getAlbumPhotoByAlbumId(((Album)album).getId());
			if(list.size()>0){
				albumPhotoList.add(list.get(0));
			}
		}
		this.getRequest().setAttribute("albumPhotoList", albumPhotoList);
		return Action.SUCCESS;
	}	
	
	/**
	 * view Album
	 * @return
	 */
	public String viewAlbum(){
		String albumId = this.getRequest().getParameter("aId");
		if(null != albumId){
			this.getRequest().setAttribute("list", this.getAlbumPhotoService().getAlbumPhotoByAlbumId(Integer.valueOf(albumId)));
		}
		return Action.SUCCESS;
	}
	
	

	
	public AlbumService getAlbumService() {
		return albumService;
	}
	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}
	public AlbumPhotoService getAlbumPhotoService() {
		return albumPhotoService;
	}
	public void setAlbumPhotoService(AlbumPhotoService albumPhotoService) {
		this.albumPhotoService = albumPhotoService;
	}
	public PhotoService getPhotoService() {
		return photoService;
	}
	public void setPhotoService(PhotoService photoService) {
		this.photoService = photoService;
	}
	
}
